// 给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。
function ListNode(val, next) {
    this.val = (val === undefined ? 0 : val)
    this.next = (next === undefined ? null : next)
}
var removeNthFromEnd = function(head, n) {
    let res = head;
    let len = 0;
    let len1 = 0;
    while (res.next != null) {
        res = res.next;
        len++;
    }
    len++ //本题目中的头节点不为空，因此长度加一，后面len1+1也是同理
    if (len == n) {
        return head.next
    }
    let temp = head;
    if (n > 0 && n < len) {
        while (len - n != len1 + 1) {
            temp = temp.next;
            len1++
        }
        temp.next = temp.next.next;
    }

    return head;
};